﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using System.Data;

namespace iLangl.Customer.DAL.XMLStorage
{
	public class ProjectDAL : BaseStorageDAL, IProjectDAL
	{
		#region IProjectDAL Members

		public void CreateProject(CustomerProject project, IDbTransaction transaction)
		{
			dtoXmlStorage.ProjectRow projectRow = Storage.Project.FindById(project.Id);
			bool isNew = (projectRow == null);
			if (isNew)
			{
				projectRow = Storage.Project.NewProjectRow();
			}

			projectRow.Deadline = project.Deadline;
			projectRow.Description = project.Description;
			projectRow.Id = project.Id;
			projectRow.Name = project.Name;

			projectRow.CustomerIdentity = project.CustomerIdentity;
			projectRow.TPIdentity = project.TPIdentity;
			projectRow.IsLockContent = project.IsLockContentDuringTranslation;

			if(project.Owner != null)
				projectRow.OwnerId = project.Owner.Id;

			if(project.Product != null)
				projectRow.ProductId = project.Product.Id;


			projectRow.Status = project.Status;
			if (project.TechnicalContact != null) 
			projectRow.TechnicalContactId = project.TechnicalContact.Id;

			if(project.WorkArea != null)
				projectRow.WorkAreaId = project.WorkArea.Id;

			if (isNew)
			{
				Storage.Project.AddProjectRow(projectRow);
			}


			if (transaction == null)
			{
				AcceptChanges();
			}
		}


		public CustomerProject ReadProject(string projectId)
		{
			CustomerProject result = new CustomerProject();
			dtoXmlStorage.ProjectRow projectRow = Storage.Project.FindById(projectId);

			if (projectRow == null)
			{
				throw new ApplicationException(String.Format("Project with Id='{0}' not found!", projectId));
			}

			result.Id = projectRow.Id;
			result.Deadline = projectRow.Deadline;
			result.Description = projectRow.Description;
			result.Name = projectRow.Name;
			result.IsLockContentDuringTranslation = projectRow.IsLockContent;

			if (!projectRow.IsCustomerIdentityNull())
				result.CustomerIdentity = projectRow.CustomerIdentity;

			if (!projectRow.IsTPIdentityNull())
				result.TPIdentity = projectRow.TPIdentity;

			result.WorkArea = DALFacade.Instance.WorkAreaDAL.ReadWorkAreaById(projectRow.WorkAreaId);
			//result.Owner = DALFacade.Instance.ContactDAL.ReadContact(projectRow.OwnerId);			
			//result.TechnicalContact = DALFacade.Instance.ContactDAL.ReadContact(projectRow.TechnicalContactId);

			result.Product = DALFacade.Instance.ProductDAL.ReadProductById(projectRow.ProductId);
			

			return result;
		}
		#endregion


		public CustomerProject[] GetProjectsByStatus(string Status)
		{
			List<CustomerProject> result = new List<CustomerProject>();

			foreach (dtoXmlStorage.ProjectRow tr in Storage.Project)
			{
				if(tr.Status == Status)
					result.Add(ReadProject(tr.Id));
			}

			return result.ToArray();
		}


		public CustomerProject GetProjectByName(string projectTitle)
		{
			foreach (dtoXmlStorage.ProjectRow tr in Storage.Project)
			{
				if (String.Compare(tr.Name ,projectTitle.Trim(), true) == 0)
					return ReadProject(tr.Id);
			}

			return null;
		}

		
	}
}
